﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp1Demo.DB;
using WpfApp1Demo.Model;

namespace WpfApp1Demo.ViewModel
{

    public class MainViewModel : ViewModelBase   //这里要继承哦, 为了绑定通知实现
    {
     
        public MainViewModel()
        {
            //数据库对象
            localDB = new LocalDB();
            localDB.ToGetDatas("select * from [dbo].[sys_user]");
            //初始化
            userGriList = new ObservableCollection<User>();
            //query
            SearchCommand = new RelayCommand<string>(Query);
            SearchCommand.Execute(string.Empty);
        }

        private void Reset(string name)
        {
            var user = localDB.GetUser(name);

            throw new NotImplementedException();
        }

        private ObservableCollection<User> userGriList;
        public ObservableCollection<User> UserGriList //ObservableCollection 是个新东西
        {
            get => userGriList; set
            {
                userGriList = value;
                RaisePropertyChanged();
            }
        }

        private LocalDB localDB;

        public RelayCommand<string> SearchCommand { get; set; }
        public RelayCommand<string> ResetCommand { get; set; }

        public void Query(string name)
        {
            var user = localDB.GetUser(name);
            UserGriList = new ObservableCollection<User>();
            foreach (var userEnum in user)
            {
                UserGriList.Add(userEnum);
            }
        }
    }
}
